package tasks 
{
	import app.AppModel;
	import app.modules.TreeFolderInfo;
	import app.textures.TextureInfoGroupe;
	import com.bit101.components.WheelMenu;
	import flash.events.Event;
	import flash.utils.Dictionary;
	/**
	 * ...
	 * @author Dmitriy Gaiduk
	 */
	public class LoadMainXml extends BaseXmlLoadTask
	{
		private var _model:AppModel;
		private var _tasks:Vector.<BaseTask>;
		private var _taskCompleted:int;
		private var _allTaskCompleted:Boolean;
		
		public function LoadMainXml(xmlFile:String, model:AppModel) 
		{
			super(xmlFile);
			_model = model;
		}
		
		override protected function onXMLReaded(xml:XML):void 
		{
			var list:XMLList;
			var furnFileName:String = "";
			var buttonsFurnBottomFileName:String = "";
			_tasks = new Vector.<BaseTask>();
			var task:BaseXmlLoadTask;
			var count:int;
			// read buttons name
			try
			{
				/*_model.btnRoomSize_Title = xml.rightbar[0].roomsize[0].attribute("title");
				_model.btnBottomFurn_Title = xml.rightbar[0].bottomfurnitures[0].attribute("title");
				_model.btnTopFurn_Title = xml.rightbar[0].topfurnitures[0].attribute("title");
				_model.btnAppliances_Title = xml.rightbar[0].appliances[0].attribute("title");
				_model.btnFacadeTexture_Title = xml.bottombar[0].facade[0].attribute("title");
				_model.btnTabletopTexture_Title = xml.bottombar[0].tabeletop[0].attribute("title");
				*/
				
				//gallery link
				if (xml.elements("gallery").length() > 0) {
					_model.galleryURL = xml.elements("gallery")[0].attribute("url");
				}
				
				// link video
				if (xml.elements("video").length() > 0) {
					_model.tutorVideoUrl = xml.elements("video")[0].attribute("source");
				}
				
				// add favorite function
				if (xml.elements("favorites").length() > 0) {
					_model.addFavoritesFunc = xml.elements("favorites")[0].attribute("function");
					log("favorites: " + _model.addFavoritesFunc);
				}
				
				// load furnitures
				_model.furnituresDict = new Dictionary();
				if ( xml.elements("furnitures").length() > 0)
				{
					var furnXmlCount : int  = xml.elements("furnitures").length();
					for (var i:int = 0; i < furnXmlCount; i++ )
					{
						if (xml.furnitures[i].attribute("source").length() > 0)
						{
							task = new LoadFurnitures(xml.furnitures[i].attribute("source"), _model);
							_tasks.push(task);
						}
					}
				}
				// load rigth bar buttons
				if ( xml.elements("rightbar").length() > 0)
				{
					// bottom furnitures
					_model.bottomFurnitures = new Vector.<TreeFolderInfo>();
					if (xml.rightbar[0].elements("bottomfurnitures").length() > 0)
					{
						if (xml.rightbar[0].bottomfurnitures[0].attribute("source").length() > 0)
						{
							 task = new LoadButtons(xml.rightbar[0].bottomfurnitures[0].attribute("source"), 
													_model.bottomFurnitures);
							_tasks.push(task);
						}
					}
					
					// top furnitures
					_model.topFurnitures = new Vector.<TreeFolderInfo>();
					if (xml.rightbar[0].elements("topfurnitures").length() > 0)
					{
						if (xml.rightbar[0].elements("topfurnitures")[0].attribute("source").length() > 0)
						{
							 task = new LoadButtons(xml.rightbar[0].elements("topfurnitures")[0].attribute("source"), 
													_model.topFurnitures);
							_tasks.push(task);
						}
					}
					// tech furnitures
					_model.techFurnitures = new Vector.<TreeFolderInfo>();
					if (xml.rightbar[0].elements("techfurnitures").length() > 0)
					{
						if (xml.rightbar[0].elements("techfurnitures")[0].attribute("source").length() > 0)
						{
							 task = new LoadButtons(xml.rightbar[0].elements("techfurnitures")[0].attribute("source"), 
													_model.techFurnitures);
							_tasks.push(task);
						}
					}
				}
				
				//load bottom bar 
				if ( xml.elements("bottombar")[0].length() > 0)
				{
					// load facade textures
					if (_model.facadeTextures == null)
					{
						_model.facadeTextures = new Vector.<TextureInfoGroupe>();
					}
					loadTextures(xml.elements("bottombar")[0], "facade", _model.facadeTextures );
					
					//tabletop textures
					if (_model.tabletopTextures == null)
					{
						_model.tabletopTextures = new Vector.<TextureInfoGroupe>();
					}
					loadTextures(xml.elements("bottombar")[0], "tabeletop", _model.tabletopTextures );
				}
				
				//load additionally
				if ( xml.elements("additionally").length() > 0)
				{
					//tech textures
					var textureVect:Vector.<TextureInfoGroupe> = new Vector.<TextureInfoGroupe>();
					loadTextures(xml.elements("additionally")[0], "tech", textureVect );
					for (i = 0; i < textureVect.length; i++)
					{
						if (textureVect[i].targetSurface == TextureInfoGroupe.TARGET_SIDE_TECH)
						{
							_model.techTextureGroupe = textureVect[i];
							break;
						}
					}
				}
			}
			catch(err:Error)
			{
				handleError("Error while read images from XML: " + err.errorID + "; " + err.name + "; "  + err.message);
				_model.inited = false;
				complete(false);
				return;
			}
			
			// run tasks
			log("run tasks");
			_taskCompleted = 0;
			_allTaskCompleted = true;
			for each(var taskForRun:BaseTask in _tasks)
			{
				taskForRun.addEventListener(Event.CHANGE, handleTaskChange);
				taskForRun.run();
			}
			
		}
		
		private function loadTextures(xml:XML, element:String, targetArr:Vector.<TextureInfoGroupe>):void
		{
			if (xml.elements(element).length() > 0)
					{
						var count:int =  xml.elements(element)[0].elements("textures").length();
						
						for (var i:int=0 ; i < count; i++)
						{
							if (xml.elements(element)[0].textures[i].attribute("source").length() > 0)
							{
								var textureGroupe:TextureInfoGroupe = 
								new TextureInfoGroupe(xml.elements(element)[0].textures[i].attribute("title"),
														null);
								textureGroupe.displayType = 
									xml.elements(element)[0].textures[i].attribute("display");
								
								textureGroupe.buttonStyle = 
									xml.elements(element)[0].textures[i].attribute("style");
								
								textureGroupe.targetSurface = 
									xml.elements(element)[0].textures[i].attribute("target");
								
								textureGroupe.textureType = 
									xml.elements(element)[0].textures[i].attribute("type");
								
								
								textureGroupe.previewHeight = 
								getNumberFromStr(xml.elements(element)[0].textures[i].attribute("height"),
												textureGroupe.previewHeight, "preview height NaN", true);
												
								textureGroupe.previewWidth = 
								getNumberFromStr(xml.elements(element)[0].textures[i].attribute("width"),
												textureGroupe.previewWidth, "preview width NaN", true);
								
								targetArr.push(textureGroupe);
								 var task:BaseTask = new 
								 LoadTexture(xml.elements(element)[0].textures[i].attribute("source"),
														textureGroupe);
								_tasks.push(task);
							}
						}
					}
		}
		
		private function handleTaskChange(e:Event):void 
		{
			
			_taskCompleted++;
			var task:BaseTask = e.currentTarget as BaseTask;
			if (task != null)
			{
				task.removeEventListener(Event.CHANGE, handleTaskChange);
				if (!task.ready)
				{
					_allTaskCompleted = false;
				}
			}else
			{
				handleError("Completed task is null");
			}
			
			if (_taskCompleted == _tasks.length)
			{
				complete(_allTaskCompleted);
			}
		}
		
		override public function destroy():void 
		{
			_model = null;
			for each(var task:BaseTask in _tasks)
			{
				task.removeEventListener(Event.CHANGE, handleTaskChange);
				task.destroy();
			}
			_tasks = null;
			super.destroy();
		}
	}

}